<template>
  <div>
    <form>
      <slot></slot>
    </form>
  </div>
</template>

<script>
const findChildren = (parent, componentName, result) => {
  let res = result || []
  const children = parent.$children
  console.log(children)
  children.forEach(child => {
    if(child.$options.name === componentName) {
      res.push(child)
    }else{
      res = res.concat(findChildren.apply(child, componentName, res))
    }
  })
  return res
}
export default {
  name: 'LgForm',
  provide() {
    return {
      form: this
    }
  },
  props: {
    model: {
      type: Object
    },
    rules: {
      type: Object
    }
  },
  methods: {
    validate (cb) {
      const children = findChildren(this, 'LgFormItem')
      const tasks = children
        .filter(child => child.prop)
        .map(child => child.validate())
      Promise.all(tasks)
        .then(() => cb(true))
        .catch(() => cb(false))
    }
  }
}
</script>

<style>

</style>